import os
logfile = open('log.txt', mode='a')
logfile.writelines('my line\n')
logfile.close()

os.rename('log.txt', 'lognew.txt')